// $codepro.audit.disable
package edu.gatech.teama.doctorsoffice.test;


import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import edu.gatech.teama.doctorsoffice.model.Address;
import edu.gatech.teama.doctorsoffice.model.Doctor;
import edu.gatech.teama.doctorsoffice.model.Gender;
import edu.gatech.teama.doctorsoffice.model.Nurse;
import edu.gatech.teama.doctorsoffice.model.Patient;
import edu.gatech.teama.doctorsoffice.model.PatientInstructions;
import edu.gatech.teama.doctorsoffice.model.User;
import edu.gatech.teama.doctorsoffice.net.AddUserMessage;
import edu.gatech.teama.doctorsoffice.net.DeleteUserMessage;
import edu.gatech.teama.doctorsoffice.net.LoginMessage;
import edu.gatech.teama.doctorsoffice.net.LoginResult;
import edu.gatech.teama.doctorsoffice.net.LookupPatientMessage;
import edu.gatech.teama.doctorsoffice.net.Message;
import edu.gatech.teama.doctorsoffice.net.QuitMessage;
import edu.gatech.teama.doctorsoffice.net.ReadUserMessage;
import edu.gatech.teama.doctorsoffice.net.UpdateUserMessage;
import edu.gatech.teama.doctorsoffice.server.DataModel;
import edu.gatech.teama.doctorsoffice.server.SerializedDataModel;
import edu.gatech.teama.doctorsoffice.server.Server;
//import edu.gatech.teama.doctorsoffice.server;

public class emily_test {

	private Server srv;
	Socket clientSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	ExecutorService pool;
	final String testDataFileName = "test.dat";
	
	public void startServer() {
		try {
			// Execute server on new thread
			srv = new Server(4123, testDataFileName);
			pool = Executors.newSingleThreadExecutor();
			pool.execute(srv);

			// Give the server some time to start
			Thread.sleep(100);

			clientSocket = new Socket("localhost", 4123);

			out = new ObjectOutputStream(clientSocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(clientSocket.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Before
	public void setUp() throws Exception {
		// Delete data file if a previous one exists
		File f = new File(testDataFileName);
		if (f.exists()) {
			f.delete();
		}
		
		startServer();
	}

	@After
	public void tearDown() throws Exception {
		out.writeObject(new QuitMessage());
		clientSocket.close();
		pool.shutdownNow();
	}
	
	@Test
	public void testServer() {
		User newUser = new Nurse("testname");
		try {
			out.writeObject(new AddUserMessage(newUser, "testpass"));
			Message mess = (Message) in.readUnshared();
			Assert.assertTrue((Boolean)mess.getPayload());
			
			out.writeObject(new ReadUserMessage("testname"));
			mess = (Message) in.readUnshared();
			User returnedUser = (User)mess.getPayload();
			System.out.println("User " + returnedUser.getUsername());
			Assert.assertEquals(newUser.getUsername(), returnedUser.getUsername());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

//	@Test
//	public void testUser() {
//		DataModel sd = new SerializedDataModel("testData.dat");
//		Address testAddress = new Address(1234, "4th Street", 90210, "Atlanta", "GA");
//		User testUser = new Nurse("testUser", "John", "Cliff", "123-456-7890", testAddress, "testEmail@gmail.com");
//		sd.addUser(testUser, "testpass");
//		Assert.assertEquals(sd.readUser("testUser"), testUser);
//		Assert.assertEquals(sd.readUser("testUser").getFirstName(), testUser.getFirstName());
//		Assert.assertEquals(sd.readUser("testUser").getLastName(), testUser.getLastName());
//		Assert.assertEquals(sd.readUser("testUser").getUsername(), testUser.getUsername());
//		Assert.assertEquals(sd.readUser("testUser").getAddress(), testUser.getAddress());
//		Assert.assertEquals(sd.readUser("testUser").getPhoneNumber(), testUser.getPhoneNumber());
//		Assert.assertEquals(sd.readUser("testUser").getEmailAddress(), testUser.getEmailAddress());
//	
//	/*
//	sd.deleteUser("testUser");
//	Address testAddress1 = new Address(12341, "4th Street1", 902101, "Atlanta1", "GA1");
//	Calendar cal = Calendar.getInstance();
//    cal.set(Calendar.YEAR, 1980);
//    cal.set(Calendar.MONTH, 0);
//    cal.set(Calendar.DATE, 2);
//	Patient testUser1 = new Patient("testUser1", "Cliff1", "John1", "123-456-78901", testAddress1, "testEmail@gmail.com1", Gender.FEMALE, cal, "Publix", "Blue Cross Blue Shield");
//	Assert.assertEquals(sd.readUser("testUser1"), testUser);
//	Assert.assertEquals(sd.readUser("testUser1").getFirstName(), testUser1.getFirstName());
//	Assert.assertEquals(sd.readUser("testUser1").getLastName(), testUser1.getLastName());
//	Assert.assertEquals(sd.readUser("testUser1").getUsername(), testUser1.getUsername());
//	Assert.assertEquals(sd.readUser("testUser1").getAddress(), testUser1.getAddress());
//	Assert.assertEquals(sd.readUser("testUser1").getPhoneNumber(), testUser1.getPhoneNumber());
//	Assert.assertEquals(sd.readUser("testUser1").getEmailAddress(), testUser1.getEmailAddress());
//	Assert.assertEquals(sd.readUser("testUser1").getPharmacy(), testUser1.getPharmacy());
//	Assert.assertEquals(sd.readUser("testUser1").getInsuranceCarrier(), testUser1.getInsuranceCarrier());
//	Assert.assertEquals(sd.readUser("testUser1").getGender(), testUser1.getGender());
//	*/
//	}
}	
